//
//  PopoverAnimater.swift
//  Status
//
//  Created by 花轮 on 15/5/15.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit

class PopoverAnimater: NSObject,UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
{
    // 是否正在展现的标记
    var isPresentation = false
    var presentedFrame = CGRectZero
    
    var titleBtn : UIButton?
    
    // 返回控制modal管理的控制器
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let popover = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        
        popover.presentedFrame = presentedFrame
        
        return popover
    }
    
    // 返回提供modal展现动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresentation = true
        return self
    }
    
    // 返回提供dismiss动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresentation = false
        
        return self
    }
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.5
    }
    
    // transitionContext:转场上下文，提供转场动画的相关信息，从哪里来，到哪里去
    // 主动提供转场的动画，一旦实现了这个方法，原有的转场动画会失效  目标控制器就看不到了
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresentation
        {
            titleBtn!.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
            
            // 讲toVc的View添加到容器视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            // 将目标视图添加到容器视图
            transitionContext.containerView().addSubview(toView)
            
            // 动画方法
            // 设置动画初始的形变
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            // 设置图层的锚点
            toView.layer.anchorPoint = CGPointMake(0.5, 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: nil, animations:
                { () -> Void in
                    
                    toView.transform = CGAffineTransformMakeScale(1.0, 1.0)
                    
                }, completion: { (_) -> Void in
                    // 一定要执行的函数,告诉系统转场动画结束的
                    transitionContext.completeTransition(true)
            })
        }
        else{
            titleBtn!.setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Normal)

            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            // 将modal出来的视图从容器视图删除
            fromView.removeFromSuperview()
            
            transitionContext.completeTransition(true)
        }
        
    }

}
